jQuery(function() {
    //init Homepage SlideShow
  
    initSlideShow();

})
/******************************
 ^INIT HOMEPAGE SLIDESHOW
 ********************************/
function initSlideShow() {
    var slides = jQuery("#slideshow .slide"),
        controls = jQuery(".nav li", '#slideshow'), 
        sideControls = jQuery(".sideNav a", '#slideshow'), 
        reel = jQuery("#reel"), 
        inProgress = false, 
        autoPlay = true, 
        slideWidth = 749, 
        timeInterval = 7000, //Wait between slides
        slideSpeed = 500, // slide speed
        statusVideo = 'stop', // Status of video [stop / play]
        player;

    //Auto Slideshow
    function startTimer() {
        if(statusVideo === 'stop') {
            play = setInterval(function() {
                var current = slides.filter('.active').index();
                if(current >= 1) {// CHANGE NUMBER TO ONE LESS THAN THE NUMBER OF SLIDES IN SLIDESHOW
                    autoPlay = false;
                    clearInterval(play);
                }
                if(autoPlay) {
                    var current = slides.filter('.active').index();
                    moveNext(current + 1, current);
                }
            }, timeInterval);
        }

    }

    function moveNext(current, previous) {
        if(!inProgress) {
            inProgress = true;
            controls.removeClass('active');
            controls.eq(current).addClass('active');
            slides.eq(current).addClass('active').css({
                'display' : 'block',
                'left' : slideWidth + 'px'
            }).animate({
                left : 0
            }, slideSpeed);

            slides.eq(previous).animate({
                left : '-' + slideWidth + 'px'
            }, slideSpeed, function() {
                jQuery(this).removeClass('active');
                inProgress = false;
            });
        }

    }

    function movePrevious(current, next) {
        if(!inProgress) {
            controls.removeClass('active');
            controls.eq(current).addClass('active');
            slides.eq(current).addClass('active').css({
                'display' : 'block',
                'left' : '-' + slideWidth + 'px'
            }).stop().animate({
                left : 0
            }, slideSpeed);

            slides.eq(next).stop().animate({
                left : slideWidth + 'px'
            }, slideSpeed, function() {
                jQuery(this).removeClass('active');
                inProgress = false;
            });
        }
    }

    // Arrow Navigation
    sideControls.click(function(evt) {
        clearInterval(play);
        var current = slides.filter('.active').index(), previous = 0, next = 0;
        var total = slides.length - 1 ;

        // NEXT
        if(jQuery(this).index() == 1) {
            if(current == total) {
                current = 0;
                previous = total;
            } else {
                current++;
                previous = current - 1;
            }
            moveNext(current, previous);
        }

        // PREVIOUS
        if(jQuery(this).index() == 0) {
            if(current == 0) {
                current = total;
                next = 0;
            } else {
                current--;
                next = current + 1;
            }
            movePrevious(current, next);
        }
        
       
        evt.preventDefault();
    });
    
    // Points Navigation
    controls.click(function(evt) {
        var current = slides.filter('.active').index();
        var next = jQuery(this).index();
        if(next > current) {
            clearInterval(play);
            moveNext(next, current);
        }
        if(next < current) {
            clearInterval(play);
            movePrevious(next, current);
        }

        
        evt.preventDefault();
    });
    
    // Startup
    slides.first().addClass("active");
    controls.first().addClass("active");
    startTimer();

    // Stop sliding on mouseover, on mouseout resume slideshow
    slides.hover(function() {
        clearInterval(play);
    }, function() {
        startTimer();
        inProgress = false;
    });



}